First, we'll cover Cloud SQL, because a relational database is the center piece for
most applications. In case you're not familiar with Cloud SQL, here are
the highlights. Cloud SQL is a fully managed
relational database service. Cloud SQL offers
fully-managed MySQL, PostgreSQL, and SQL server. This means you'll have
a clear migration path with low switching costs if you want to replace Cloud SQL with a self-hosted alternative. Cloud SQL supports
scheduled backups, replication, encryption, and automated storage
capacity increase. You can enable
automatic failover to increase availability. The service level agreement
for availability is greater than 99.95 percent. Let's make that more specific. You'll get financial
credits if there's more than 22 minutes
of downtime per month. Downtime of less than a minute, and schedule maintenance is
not counted toward the SLA. Today, there are two
ways to connect to a Cloud SQL database
server on Google Cloud. You can connect using
Cloud SQL Auth proxy, which is a separate
program that you'll need to start on your host, or using a direct
SSL/TLS connection. You can also set up an
unencrypted direct connection, but this is not recommended. If you use the
direct connection, you'll need to set up SSL/TLS yourself and set up and
manage a client certificate. If you use the Cloud
SQL Auth proxy, it handles encryption for you. Let's take a look at how
the Cloud SQL proxy works. There are two parts
of the proxy. Let's walk through
how your application makes an SQL query. Your application sets
up a connection to a local Cloud SQL proxy client, using the standard
database protocol used by your database. The local proxy client is
a program that you run. It receives local
connections and forwards the connections to the
Cloud SQL proxy server. It also adds an access token
using a service account. The Cloud SQL proxy
server verifies the access token and retrieves the service
account identity. It checks the authorization
using cloud IAM. The role Cloud SQL client on the Cloud SQL instance is required to allow
the connection. The connection between
the client and the server is encrypted
using SSL/TLS, so that's something
that you don't have to manage yourself. At first sight,
Cloud SQL Auth proxy looks more complicated than
using a direct connection. However, on Cloud Run, the most convenient
way to connect to a Cloud SQL instance is to
use the Cloud SQL Auth proxy, because cloud Ron
manages the proxy for you and hides all of this
complexity from you. To connect to a
Cloud SQL instance in the same Google
Cloud project, you need to do three things. First, you need to configure the Cloud Run service to connect
to a Cloud SQL instance. Next, from your application, connect to a Unix domain socket. This is a special file in your container in the
directory/Cloud SQL. This is a standard way to
connect to databases and is similar to using
a TCP connection with a host name and a port. Finally, you'll need to make sure that the
service identity of your Cloud Run
service is allowed to connect to the
Cloud SQL instance. The identity should have Cloud SQL client role on
the Cloud SQL instance. Now, let's talk about scaling. Cloud Run is known
for its fast scaling. If your container
startup time is short, Cloud Run can go from 0-1,000 containers in
less than 10 seconds. Cloud SQL can also scale to handle serious
production workloads. As of May 2021, the largest Cloud
SQL instance has 96 vCPUs with 6.5 gigabytes
of memory per vCPU. This is likely to
change in the future. However, Cloud SQL requires a power cycle or a
failover to scale. That means that your
Cloud SQL instance might not be ready to handle
the demand when it comes. With traditional
relational databases, resource contention
causes performance to decrease if many transactions are happening at the same time. Cloud Run can generate
a lot of concurrency. Because it can scale by
adding more containers. Every container handles requests and the request calls
queries to the database, which causes transactional
concurrency. Look at this chart for a
visual representation. At the start, more clients cause an increase in
transaction rate. That's how it's
supposed to work. At some point, resource
contention starts to occur and transaction duration
increases while transaction rates
stops increasing. Your queries will start
to become slower. When more and more
clients show up, transaction rate might
even start decreasing, lowering the capacity
even further and accelerating the increase
of transaction duration. The key to maintaining
performance is managing the number of
concurrent transactions. On traditional servers, we've been doing this for years
using connection pooling. A connection pool provides an upper limit to the number
of concurrent transactions, because there's a fixed number
of connections in a pool. On Cloud Run, you can also
use connection pooling, but there's an additional
way to limit concurrency. Using the maximum
instance setting. If you use connection pooling, you can add a connection pool on the individual container, or use an external
connection pool hosted on compute engine, but that's really for
advanced scenarios. If you limit the maximum
number of containers, you're limiting the
maximum number of requests that can be
handled at the same time. This may result in request queuing when your
Cloud Run service is at maximum capacity and you might see rejected
request or a timeout, but your Cloud SQL instance
won't be overloaded. How do you find the right
values for these limits? What's the best size
for a connection pool? What's the right maximum
instance setting? There's no single answer
to these questions. You'll need to find out what
works for your workload. Use load testing to simulate
high traffic situations, and closely monitor
the health of your Cloud SQL instance
in production. Choice of my scaling
configuration for Cloud Run and Cloud SQL. The first part, pick the right
Cloud SQL instance size, monitor the load, and resize to a bigger instance if
you expect peak traffic. Resizing is fully automated, but it does require a
restart, a failover. Don't forget the second part. Due to the way traditional relational databases
are designed, a large number of
concurrent transactions has a detrimental
effect on performance. In fact, you can often achieve higher transaction rates if you limit the number of concurrent transactions than if you don't. Application developers
have been doing exactly this for years
using connection pooling. Additionally, you can
also limit concurrency by setting a maximum number
of container instances. If Cloud Run reaches
a maximum limit, it will queue incoming
requests until a container becomes available
to handle the request. Regardless of how you configure the request concurrency
or the instance size, you'll always need to monitor the health of a
Cloud SQL instance, and keep an eye
on the CPU usage, disk usage, and
query performance. Those operational
responsibilities are not gone when you use Cloud SQL. Cloud SQL is a fully
managed relational database service for MySQL, PostgreSQL, and SQL server. Connect from Cloud
Run to the Cloud SQL using the built-in
Cloud SQL proxy. Maintain optimal
performance using connection pooling and
maximum instances.